package org.supper.auth.exception;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

public class RestApiExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(value = {FeignBadRequestException.class})
    public ResponseEntity<Object> handleFeignException(FeignBadRequestException ex, WebRequest request) {
        return handleExceptionInternal(ex, buildFeignResponse(new FeignBadRequestException(ex.getCode(), ex.getMessage())),
                new HttpHeaders(), HttpStatus.valueOf(ex.getCode()), request);
    }

    private String buildFeignResponse(FeignBadRequestException ae) {

        return ae.getMessage();
    }
}